/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.OutletManagement;

import session.TX.DishManagerSBRemote;
import entity.tx.FoodItem;
import exception.tx.DishNamingConflictException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean(name = "foodItemManagedBean")
@ViewScoped
public class FoodItemMB {

    @EJB
    DishManagerSBRemote dishBean;
    @ManagedProperty(value = "#{photoManagedBean}")
    private PhotoMB photoMB;
    private Long id;
    private String name;
    private String description;
    private Double cost;
    private String itemType;
    private List<FoodItem> itemList = null;
    private String statusMsgStr = "";

    /** Creates a new instance of AlaCarteMgmtBean */
    public FoodItemMB() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Double getCost() {
        return cost;
    }

    public void setCost(Double cost) {
        this.cost = cost;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getItemType() {
        return itemType;
    }

    public void setItemType(String itemType) {
        this.itemType = itemType;
    }

    public String getStatusMsgStr() {
        return statusMsgStr;
    }

    public void setStatusMsgStr(String statusMsgStr) {
        this.statusMsgStr = statusMsgStr;
    }

    public void addNewFoodItem(ActionEvent event) {
        itemList = null;
        try {
            dishBean.addFoodItem(retrieveFromSessionOutletID(), name, description, cost, itemType, getPhotoMB().getPhotoFilePath());
            statusMsgStr = "Dish Successfully Added!";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, getStatusMsgStr(), ""));
            this.name=null;
            this.cost=null;
            this.itemType="Menu Item";
            this.description=null;
        } catch (DishNamingConflictException e) {
            statusMsgStr = e.toString();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, getStatusMsgStr(), ""));
        }
    }

    public static Boolean strToboolean(String s) {
        if (s.equalsIgnoreCase("true")) {
            return true;
        } else {
            return false;
        }
    }

    public void modifyFoodItem(ActionEvent event) {
        itemList = null;
        dishBean.editFoodItem(id, name, description, cost);
    }

    public List<FoodItem> getFoodItem() {
        if (this.itemList == null) {
            itemList = this.getFoodItemFrmDatabase();
        }
        return itemList;
    }

    private List<FoodItem> getFoodItemFrmDatabase() {
        return dishBean.getOutletFoodMenu(retrieveFromSessionOutletID());
    }

    public void deleteFoodItem(ActionEvent event) {
        itemList = null;
        Long id = (Long) event.getComponent().getAttributes().get("itemId");
        dishBean.deleteFoodItem(id, retrieveFromSessionOutletID());
    }

    public void onRowEdit(RowEditEvent event) {
        FoodItem item = (FoodItem) event.getObject();
        System.out.println(item);
        dishBean.editFoodItem(item);
    }

    public String getOutletName() {
        return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("outletName").toString();
    }

    public List<String> getAvailableFoodTypes() {
        List returnList = new ArrayList<String>();
        returnList.add("Menu Item");
        returnList.add("Featured Dish");
        returnList.add("Promotional Item");
        returnList.add("Festive Item");
        returnList.add("Reward Meals");
        return returnList;
    }

    //Randall: Added this method to pull out the outlet ID from session
    public Long retrieveFromSessionOutletID() {
        return (Long) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("outletId");
    }

    public PhotoMB getPhotoMB() {
        return photoMB;
    }

    public void setPhotoMB(PhotoMB photoMB) {
        this.photoMB = photoMB;
    }
}
